<template>
  <nav class="app-topnav">
    <div class="container">
      <ul>
        <template v-if="profile.token">
          <li><RouterLink to="/member"><i class="iconfont icon-user"></i>{{ profile.account }}</RouterLink></li>
          <li><a @click="logout" href="javascript:;">退出登录</a></li>
        </template>
        <template v-else>
          <li>
            <RouterLink to="/login">请先登录</RouterLink>
          </li>
          <li><a href="javascript:;">免费注册</a></li>
        </template>
        <li><RouterLink to="/member/order">我的订单</RouterLink></li>
        <li><RouterLink to="/member" >会员中心</RouterLink></li>
        <li><a href="javascript:;">帮助中心</a></li>
        <li><a href="javascript:;">关于我们</a></li>
        <li><a href="javascript:;"><i class="iconfont icon-phone"></i>手机版</a></li>
      </ul>
    </div>
  </nav>
</template>
<script>
import { useStore } from 'vuex'
import { computed, watch } from 'vue'
import { useRouter } from 'vue-router'
export default {
  name: 'AppTopnav',
  setup() {
    const store = useStore()
    // 这种方式获取的数据不是响应式的数据
    // const { profile } = store.state.user
    // 获取响应式数据得用计算属性来获取
    const profile = computed(() => {
      return store.state.user.profile
    })
    // 退出
    const logout = () => {
      store.commit('user/setUser', {})
      // 清空本地购物车数据
      store.commit('cart/setCartList', [])
    }
    const router = useRouter()
    // 侦听地址变化，记录住当前URI地址
    watch(() => router.currentRoute, () => {
      const fullPath = encodeURIComponent(router.currentRoute.value.fullPath)
      store.commit('user/setRedirectUrl', fullPath)
    }, { immediate: true, deep: true })
    return { profile, logout }
  }
}
</script>
<style scoped lang="less">
.app-topnav {
  background: #333;

  ul {
    display: flex;
    height: 53px;
    justify-content: flex-end;
    align-items: center;

    li {
      a {
        padding: 0 15px;
        color: #cdcdcd;
        line-height: 1;
        display: inline-block;

        i {
          font-size: 14px;
          margin-right: 2px;
        }

        &:hover {
          color: @xtxColor;
        }
      }

      // li后面的兄弟li
      ~li {
        a {
          border-left: 2px solid #666;
        }
      }
    }
  }
}
</style>